package com.sywb.chuangyebao.view.fragment;

import android.net.ConnectivityManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.BaseRecyclerContract;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.NetUtils;
import org.bining.footstone.utils.ToastUtils;

/**
 * 普通列表Fragment
 */
public abstract class BaseRecyclerFragment<T extends BaseRecyclerContract.Presenter> extends BaseStatisticsFragment<T> implements BaseRecyclerContract.View {

    protected RecyclerView commonRecycler;

    protected RelativeLayout commonNodata;
    protected TextView commonNodataContent;
    protected ImageView commonNodataIcon;
    protected TextView commonNodataSubtitle;

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.layout_recycler;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);

        commonRecycler = getView(R.id.common_recycler);

        commonNodata = getView(R.id.common_nodata);
        commonNodataContent = getView(R.id.common_nodata_content);
        commonNodataIcon = getView(R.id.common_nodata_icon);
        commonNodataSubtitle = getView(R.id.common_nodata_subtitle);

        if (commonNodataSubtitle != null) {
            commonNodataSubtitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    noDataClick();
                }
            });
        }

    }

    public void noDataClick() {
        if (!NetUtils.isConnected()) {
            ToastUtils.show(mActivity, "请先连接网络~");
        } else {
            if (mPresenter != null) {
                mPresenter.initData();
            }
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(ConnectivityManager.CONNECTIVITY_ACTION)})
    public void rxNetwotkChange(String data) {
        Logger.e("收到网络改变广播");
        if (mPresenter != null) {
            mPresenter.onNetWorkChange(NetUtils.isConnected());
        }
    }

    @Override
    public boolean useRxBus() {
        return true;
    }

    @Override
    public RecyclerView getRecyclerView() {
        return commonRecycler;
    }

    @Override
    public void setNoData(boolean isNoData) {
        commonNodata.setVisibility(isNoData ? View.VISIBLE : View.GONE);
        if (isNoData) {
            if (!NetUtils.isConnected()) {
                commonNodataIcon.setImageResource(R.drawable.nonetwork);
                commonNodataContent.setText("您的网络不稳定哦，请刷新重试~");
                commonNodataSubtitle.setVisibility(View.VISIBLE);

                commonNodataSubtitle.setText("刷新");
                commonNodataSubtitle.setBackgroundResource(R.drawable.round_pie_theme);
                commonNodataSubtitle.setTextColor(ContextCompat.getColor(mActivity, R.color.colorLight));
                commonNodataSubtitle.setPadding((int) getResources().getDimension(R.dimen.dp_24), (int) getResources().getDimension(R.dimen.dp_6), (int) getResources().getDimension(R.dimen.dp_24), (int) getResources().getDimension(R.dimen.dp_6));
            } else {
                commonNodataIcon.setImageResource(R.drawable.nodata_default);
                commonNodataContent.setText("当前页面没有内容哦~");
                commonNodataSubtitle.setVisibility(View.GONE);
            }
        }
    }

}
